package com.aforce3.trailertracer.scenes;

import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3d;

import com.aforce3.trailertracer.Material;
import com.aforce3.trailertracer.Ray;
import java.util.Random;

@Deprecated
public class AreaLight extends SceneGroup implements LightSource
{
	//note that sample size is the size of each the random sample. not the sample height * width
	final static int sampleSize = 10;
	
	double sampleHeight;
	double sampleWidth;
	Point3d upperLeft;
	Vector3d rightVector;
	Vector3d downVector;
	
	Material material = new Material();
	
	public AreaLight(Point3d upperLeft, Point3d upperRight, Point3d lowerRight, Point3d lowerLeft, double sampleHeight, double sampleWidth)
	{
		// TODO Auto-generated constructor stub
		super();
		//sample size is the size of the sample lights. if a sample is 1, then it is a 1x1 grid
		this.sampleHeight = sampleHeight;
		this.sampleWidth = sampleWidth;
		this.upperLeft = upperLeft;
		
		Vector3d downVector = new Vector3d(lowerLeft);
		downVector.sub(upperLeft);
		downVector.scale(1 / sampleHeight);
		
		
		Vector3d rightVector = new Vector3d(upperRight);
		rightVector.sub(upperLeft);
		rightVector.scale(1 / sampleWidth);
		
		Random r = new Random();
		Point3d lightPoint = new Point3d();
		Vector3d rightv = new Vector3d();
		Vector3d downv = new Vector3d();
		Vector3d lightVector = new Vector3d();
		Ray lightRay;
		
		PointLight pointL;
		
		//material.getEmissiveColor().scale(1 / sampleSize);
		
		//float radius = (float) Math.max(rightv.length(), downv.length());
		float radius = 0.2f;
		
		for(int i=0; i<sampleHeight; i++){
			for(int j=0; j<sampleWidth; j++){
				lightPoint.set(upperLeft);
				rightv.set(rightVector);
				downv.set(downVector);
				
				rightv.scale(j + 1 / (r.nextInt(sampleSize) + 1));
				downv.scale(i + 1 / (r.nextInt(sampleSize) + 1));
				
				lightPoint.add(rightv);
				lightPoint.add(downv);
				
				
				
				pointL = new PointLight(lightPoint, radius, material.getEmissiveColor(), 1);
				this.add(pointL);
			}
		}
	}
	
	public void setMaterial(Material m){
		material = m;
	}
	
	public Color3f getColor(Ray ray, Scene s){
		//System.out.println("got color" + material.getEmissiveColor());
		Color3f c = new Color3f(material.getEmissiveColor());
		
		return c;
	}
	
	//calculates intensity to a point
	public float getIntensity(Point3d point, Scene s)
	{
		return 1;
	}

	public float getIntensity(Point3d point)
	{
		// TODO Auto-generated method stub
		return 0;
	}

}
